package com.handinfo.android.ui.window;

import java.io.DataInputStream;
import java.io.IOException;

import android.graphics.Bitmap;

import com.handinfo.android.DWGameManager;
import com.handinfo.android.core.graphics.DWFont;
import com.handinfo.android.ui.IUIWindows;
import com.handinfo.android.ui.UIWindows;
import com.handinfo.android.uicontrols.DWControlsManager;
import com.handinfo.android.uicontrols.DWListener;
import com.handinfo.android.uicontrols.controls.DWBackground;
import com.handinfo.android.uicontrols.controls.DWButton;
import com.handinfo.android.uicontrols.controls.DWFrame;
import com.handinfo.android.uicontrols.controls.DWImageBox;
import com.handinfo.android.uicontrols.controls.DWListSubItem;
import com.handinfo.android.uicontrols.controls.DWListbox;
import com.handinfo.android.uicontrols.controls.DWMessageBox;
import com.handinfo.android.uicontrols.controls.DWTextbox;
import com.handinfo.android.uicontrols.controls.DWTitle;
import com.handinfo.android.utils.Tools;
/**
 * 每日领奖
 * @author kenny
 *
 */
public class UIMeri  implements IUIWindows{
	
	private DWFrame m_meiri_Fram; //每日窗口
	private DWTitle m_meiri_title;//每日标题
	private DWListbox m_meiri_listbox; //每日列表框
	private Bitmap box,anjian1,anjian2,meinv,denglu,m_cr_xxbj;

	private String[] str = {
			"青天白日X99 青天白日X99 青天白日X99",
			"青天白日X99 青天白日X99 青天白日X99",
			"青天白日X99 青天白日X99 青天白日X99",
			"青天白日X99 青天白日X99 青天白日X99",
			"青天白日X99 青天白日X99 青天白日X99",
			"青天白日X99 青天白日X99 青天白日X99",
			"青天白日X99 青天白日X99 青天白日X99",
	
	};
	
	private String[] strs={
			"<mark c=F3BA0C>" +"连续一天"+"</mark>",
			"<mark c=F3BA0C>" +"连续二天"+"</mark>",
			"<mark c=F3BA0C>" +"连续三天"+"</mark>",
			"<mark c=F3BA0C>" +"连续四天"+"</mark>",
			"<mark c=F3BA0C>" +"连续五天"+"</mark>",
			"<mark c=F3BA0C>" +"连续六天"+"</mark>",
			"<mark c=F3BA0C>" +"连续七天"+"</mark>",};
	
	private int ri=0;
	
	public void drawFram(){
		m_meiri_Fram = new DWFrame(DWFrame.FRAME_TYPE_FULL,true);
		m_meiri_Fram.setClickClose(false);
		
		//标题
		m_meiri_title = new DWTitle("", m_meiri_Fram);
		m_meiri_Fram.addControl(m_meiri_title);

		
		//背景
		DWBackground m_win_xbj_up_bg = new DWBackground(m_meiri_Fram.getShowWidth()-20,
				m_meiri_Fram.getShowHeight()-20);
		m_win_xbj_up_bg.setNearAnchor(m_meiri_Fram, Tools.LEFT_TOP,
				Tools.LEFT_TOP, 10, 10);
		m_meiri_Fram.addControl(m_win_xbj_up_bg);
				
		
		//美女
		DWImageBox nv = new DWImageBox(meinv);
		if(DWGameManager.AdpaterType == DWGameManager.AD_HIGH){
			//nv.setNearAnchor(m_win_xbj_up_bg, Tools.LEFT_TOP, Tools.LEFT_TOP, 10, 50+((m_win_xbj_up_bg.getShowHeight()-170)-nv.getShowHeight()>>1));
			nv.setNearAnchor(m_win_xbj_up_bg, Tools.LEFT_TOP, Tools.LEFT_TOP, 10, 60);
			nv.setShowWideHigh(285, 435);
		}else{
			nv.setNearAnchor(m_win_xbj_up_bg, Tools.LEFT_TOP, Tools.LEFT_TOP, 10, 50);
		}

		m_meiri_Fram.addControl(nv);
		
		//右侧小背景
		int h = 0;
		if(DWGameManager.AdpaterType == DWGameManager.AD_HIGH){
			h = m_win_xbj_up_bg.getShowHeight()-170;
		}else{
			h = nv.getShowHeight();
		}
		DWBackground m_xxbj_bg = new DWBackground(m_cr_xxbj, m_win_xbj_up_bg.getShowWidth()-nv.getShowWidth()-40,
				h);
		m_xxbj_bg.setNearAnchor(m_meiri_Fram, Tools.LEFT_TOP, Tools.LEFT_TOP, nv.getShowWidth()+30, 60);
		m_meiri_Fram.addControl(m_xxbj_bg);
		
		
		//标题
		DWImageBox dl = new DWImageBox(denglu);
		dl.setNearAnchor(m_meiri_title, Tools.LEFT_TOP, Tools.LEFT_TOP, m_meiri_Fram.getShowWidth()-denglu.getWidth()>>1,
				(m_meiri_title.getShowHeight()-denglu.getHeight()>>1)+10);
		m_meiri_Fram.addControl(dl);
		
		//列表
		m_meiri_listbox = new DWListbox(m_xxbj_bg.getShowWidth()-30, m_xxbj_bg.getShowHeight()-20);
		m_meiri_listbox.setLineSpacing(20);
		m_meiri_listbox.setNearAnchor(m_xxbj_bg, Tools.LEFT_TOP,
				Tools.LEFT_TOP, 15,10);
		
		DWTextbox[] tbs = new DWTextbox[str.length];
		DWTextbox[] tbst = new DWTextbox[str.length];
		DWListSubItem[] m_lsi = new DWListSubItem[str.length];
		DWImageBox[] m_ib = new DWImageBox[str.length];
		DWButton[] socialmb = new DWButton[str.length];
		
		for(int i=0;i<str.length;i++){
			 m_lsi[i] = new DWListSubItem();
			 m_ib[i] = new DWImageBox(box);
			//绘制第一天
			tbst[i] = new DWTextbox(strs[i]);
			tbst[i].setFont(DWFont.getFont(24));
			if(DWGameManager.AdpaterType == DWGameManager.AD_HIGH){
				tbst[i].setNearAnchor(m_lsi[i],  Tools.LEFT_TOP,  Tools.LEFT_TOP,
						10,m_ib[i].getShowHeight()-tbst[i].getShowHeight()>>1);
			}else{
				tbst[i].setNearAnchor(m_lsi[i],  Tools.LEFT_TOP,  Tools.LEFT_TOP,
						0,m_ib[i].getShowHeight()-tbst[i].getShowHeight()>>1);
			}
			m_lsi[i].addControls(tbst[i]);
			
			//绘制宝箱
			m_ib[i].setNearAnchor(m_lsi[i], Tools.LEFT_TOP, Tools.LEFT_TOP, tbst[i].getShowWidth()+10, 0);
			m_lsi[i].addControls(m_ib[i]);
			
			//绘制文字
			if(DWGameManager.AdpaterType == DWGameManager.AD_HIGH){
				tbs[i] = new DWTextbox(str[i],550);
				tbs[i].setFont(DWFont.getFont(24));
				if(DWGameManager.Screen_Width>800&&DWGameManager.Screen_Width<1280){
					tbs[i] = new DWTextbox(str[i],270);
					tbs[i].setFont(DWFont.getFont(20));
				}

				tbs[i].setNearAnchor(m_ib[i],  Tools.LEFT_TOP,  Tools.LEFT_TOP,
						10+m_ib[i].getShowWidth(),m_ib[i].getShowHeight()-tbs[i].getShowHeight()>>1);
				m_lsi[i].addControls(tbs[i]);
			
			}else{
				tbs[i] = new DWTextbox(str[i],210);
				tbs[i].setFont(DWFont.getFont(16));
				tbs[i].setNearAnchor(m_ib[i],  Tools.LEFT_TOP,  Tools.LEFT_TOP,
						10+m_ib[i].getShowWidth(),0);
				m_lsi[i].addControls(tbs[i]);
			}
			
			//绘制按钮
			int x=0;
			if(DWGameManager.AdpaterType == DWGameManager.AD_HIGH){
				x=-5;
			}else{
				x=10;
			}
			if(ri==i){
				socialmb[i] = new DWButton("领取", anjian1);
				socialmb[i].setDownImage(anjian2);
				socialmb[i].setNearAnchor(tbs[i], Tools.LEFT_TOP,  Tools.LEFT_TOP, x+tbs[i].getShowWidth(), 55-anjian1.getHeight()>>1);
				socialmb[i].addListener(new DWListener() {

					public void OnDoubleClick() {
					}

					public void OnClick() {
						DWControlsManager.getInstance().removeControl(m_meiri_Fram);
						DWGameManager.getInstance().getSendMessage().sendDuelMeiri();
					}
				});
				
				m_lsi[i].addControls(socialmb[i]);
				
			}else{
				if(ri>i){
					socialmb[i] = new DWButton("已获得", anjian2);
					socialmb[i].setNearAnchor(tbs[i], Tools.LEFT_TOP,  Tools.LEFT_TOP, x+tbs[i].getShowWidth(), 55-anjian1.getHeight()>>1);
					m_lsi[i].addControls(socialmb[i]);
				}else{
					socialmb[i] = new DWButton("未领取", anjian2);
					socialmb[i].setNearAnchor(tbs[i], Tools.LEFT_TOP,  Tools.LEFT_TOP, x+tbs[i].getShowWidth(), 55-anjian1.getHeight()>>1);
					m_lsi[i].addControls(socialmb[i]);
				}

			}

			
			m_meiri_listbox.addSubItem(m_lsi[i]);
			m_meiri_listbox.setShowBackgroundRect(false);
			
		}
		
		m_meiri_listbox.setScrollbar(ri);
		
		m_meiri_Fram.addControl(m_meiri_listbox);
		
		
		DWTextbox tb = new DWTextbox("<mark c=F3BA0C>" +"连续每日登陆可领取礼包"+"</mark>");
		tb.setNearAnchor(m_meiri_Fram,  Tools.LEFT_TOP,  Tools.LEFT_TOP,
				m_meiri_Fram.getShowWidth()-tb.getShowWidth()>>1,m_xxbj_bg.getShowHeight()+60+((m_meiri_Fram.getShowHeight()-(m_xxbj_bg.getShowHeight()+60))-tb.getShowHeight()>>1));
		
		m_meiri_Fram.addControl(tb);
		
		
		
		
		DWControlsManager.getInstance().addControl(m_meiri_Fram);
		
	}
	
	public void init() {
		// TODO Auto-generated method stub
		m_cr_xxbj = UIWindows.createImage("/img/newui/beibaobj_1.gnp"); // /小背景
		box = UIWindows.createImage("/img/icon/big_datubiao_91.gnp");
		anjian1 = UIWindows.createImage("/img/newui/anniu_2.gnp");
		anjian2 = UIWindows.createImage("/img/newui/anniu_2ax.gnp");
		meinv =  UIWindows.createImage("/img/newui/shangchengmn_1.gnp");
		denglu = UIWindows.createImage("/img/newui/denglujiangli_1.gnp");
	}

	public void open(byte index) {
		// TODO Auto-generated method stub
		Tools.debugPrintln("进入每日领奖");
		drawFram();
	}

	public void close(byte index) {
		// TODO Auto-generated method stub
		
	}

	public void dispose() {
		// TODO Auto-generated method stub
		
	}

	public void logic() {
		// TODO Auto-generated method stub
		
	}
	
	public void recvMeiri(DataInputStream dis) throws IOException {
		Tools.debugPrintln("进入读取每日奖励dis");
		byte size = dis.readByte();
		
		str = new String[size];
		for(int i=0;i<size;i++){
			str[i] = dis.readUTF();
		}
		ri = (int)dis.readByte();
		Tools.debugPrintln("每日日期："+ri);
		drawFram();
	}

}
